package it.gameplanet.client.widgets.support;

import it.gameplanet.client.GamePlanetContext;

import java.util.Map;

import com.google.gwt.event.dom.client.ContextMenuEvent;
import com.google.gwt.event.dom.client.ContextMenuHandler;
import com.google.gwt.event.dom.client.HasContextMenuHandlers;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.Label;

public class ContextMenuLabel extends Label implements HasContextMenuHandlers {

	public ContextMenuLabel(String text) {
		super(text);
		sinkEvents(Event.ONCONTEXTMENU);
		//addStyleName("gwt-TreeItem-GamePlanet");
	}
	
	public ContextMenuLabel(String text, final Map<String, Command> menu) {
		this(text);
		
		if(GamePlanetContext.isAdminUser()) {
			addContextMenuHandler(new AbstractContextMenuHandler() {
				@Override
				protected Map<String, Command> getMenuBar() {
					return menu;
				}
			});
		}
	}
		
	 
	@SuppressWarnings("deprecation")
	@Override
	public void onBrowserEvent(Event event) {
		event.cancelBubble(true);//This will stop the event from being propagated
		event.preventDefault();			
		  
		super.onBrowserEvent(event);
	}

	@Override
	public HandlerRegistration addContextMenuHandler(ContextMenuHandler handler) {
		return addDomHandler(handler, ContextMenuEvent.getType());
	}	
}
